package com.yumeng.common.utils;

import cn.hutool.core.util.ReUtil;
import com.yumeng.common.exception.YumengException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;

/**
 * @author wxd
 * @date 2024/11/11 13:46
 */
public class PathUtils {

    /**
     * 获取静态资源路径
     * 要么为空，要么以/开头
     * @param webMvcProperties
     * @return
     */
    public static String getStaticPath(WebMvcProperties webMvcProperties){
        if (webMvcProperties == null){
            throw new YumengException("webMvcProperties is null");
        }
        String statPath = webMvcProperties.getStaticPathPattern();
        if (StringUtils.isNotBlank(statPath)) {
            if (statPath.endsWith("/**")) {
                statPath = statPath.substring(0, statPath.lastIndexOf("/"));
            }
            if (!statPath.startsWith("/")) {
                statPath = "/" + statPath;
            }
            //如果路径中仅包含
            if (ReUtil.isMatch("^[/*]*$", statPath)){
                statPath = "";
            }
        }else{
            statPath = "";
        }
        return statPath;
    }

    /**
     * 去除url前缀
     * @param url
     * @return
     */
    public static String removeUrlPrefix(String url){
        if (StringUtils.isNotBlank(url)){
            while (url.startsWith("/")){
                url = url.substring(1);
            }
        }
        return url;
    }

}
